//一般直接写在一个js文件中
layui.use(['element','form'],function () {
    var $ = layui.$,
        form=layui.form;
        layer = layui.layer;

        $("#roundsEnd").on('click',function () {
            var recordId=$("#recordId").val();
           var params="id="+recordId;
            $.ajax({
                type: 'post',
                url: '/roundsRecord/update',
                dataType: 'json',
                data: params,
                success: function () {
                    location.href ="/roundsRecord";
                }
            })
        });


    $(".layui-btn-radius").on('click',function () {
     var buildingNo=$(this).data("buildingno");
     var buildingId=$(this).data("buildingid");
     var floor=$(this).data("floor");
     var roomNo=$(this).data("roomno");
     var butId=$(this)[0].id;
     var  params ="buildingNo=" +  buildingNo+"&roomNo="+roomNo;
     var recordId=$("#recordId").val();

        $.ajax({
            type: 'post',
            url: '/checkinrecords/getElderly',
            dataType: 'json',
            data: params,
            success: function (result) {
                if (result.data.length==0){
                    layer.msg("该房间未入住老人");
                } else{
                    var ht='';
                    for(var i=0;i<result.data.length;i++){
                        var  param ="id=" +  result.data[i].elderlyId;
                        $.ajax({
                            type: 'post',
                            url: '/elderly/selectById',
                            dataType: 'json',
                            data: param,
                            async: false,
                            success: function (d) {
                                var h=   ' <td><input hidden name="elderlyId" value="' +result.data[i].elderlyId + '"> <img style="width:100px;height:100px;border-radius: 50%;" src="/storage/files/'  +d.data.avatar +'" alt="头像" style="cursor: pointer;"><input style=" width: 101px;" type="text" name="elderlyName" readonly autocomplete="off" class="layui-input" value="' +result.data[i].elderlyName + '"></td>\n' ;
                                if (d.data.avatar==""){
                                    h=   ' <td> <input hidden name="elderlyId" value="' +result.data[i].elderlyId + '"> <img  style="width:100px;height:100px;border-radius: 50%;" src="/static/imgs/elder.png" alt="头像" style="cursor: pointer;"><input type="text"  style=" width: 101px;" name="elderlyName" readonly autocomplete="off" class="layui-input" value="' +result.data[i].elderlyName + '"></td>\n' ;
                                }
                                ht+=    '<tr>\n' +
                                    h+
                                    ' <td><div class="layui-form-item">\n' +
                                    '<input hidden name="elderNo" value="'+d.data.idNo+'"></input>'+
                                    '    <label class="layui-form-label">老人情况</label>\n' +
                                    '    <div class="layui-input-block">\n' +
                                    '      <select name="roundsStatus" lay-verify="required">\n' +
                                    '        <option value="0">正常</option>\n' +
                                    '        <option value="1">异常</option>\n' +
                                    '      </select>\n' +
                                    '    </div>\n' +
                                    '  </div>\n'+
                                    '<div class="layui-form-item layui-form-text">\n' +
                                    '    <label class="layui-form-label">异常情况</label>\n' +
                                    '    <div class="layui-input-block">\n' +
                                    '      <textarea name="deviantRemark" placeholder="请输入异常情况" class="layui-textarea"></textarea>\n' +
                                    '    </div>\n' +
                                    '  </div><div class="layui-form-item layui-form-text">\n' +
                                    '    <label class="layui-form-label">处理结果</label>\n' +
                                    '    <div class="layui-input-block">\n' +
                                    '      <textarea name="deviantResult" placeholder="请输入处理结果" class="layui-textarea"></textarea>\n' +
                                    '    </div>\n' +
                                    '  </div></td>\n' +
                                    '                       </tr>\n'
                            }
                        });
                    };
                    var html =
                        '<form class="layui-form" id="openForm">' +
                        '  <table class="layui-table">\n' +
                        '                        <colgroup>\n' +
                        '                            <col width="100">\n' +
                        '                            <col width="200">\n' +
                        '                            <col >\n' +
                        '                        </colgroup>\n' +
                        '                        <tbody>\n' +
                        ht+
                        '                       </tbody>\n' +
                        '                    </table>' +
                        '</form>';
                    layer.open({
                        title: '老人情况'
                        , area: ['650px', '600px']
                        , btn: ['保存', '取消']
                        ,type: 1
                       , content: html
                        ,success: function(layero,index){
                            form.render(); //更新全部
                        }
                        , yes: function (index, layero) {
                            var params = $("#openForm").serialize();
                            params +="&recordId=" + recordId+"&roomNo="+roomNo+"&buildingNo="+buildingNo+"&floor="+floor+"&buildingId="+buildingId;
                            $.ajax({
                                type: 'post',
                                url: '/roundsRecord/saveList',
                                dataType: 'json',
                                data: params,
                                success: function (rs) {
                                        layer.msg("完成查房",{
                                            time: 1000
                                        },function () {
                                            $("#"+butId).attr("class","layui-btn layui-btn-radius");
                                            $("#"+butId).attr("disabled","disabled");
                                            layer.close(index);
                                        });
                                }
                            });
                        }
                    });
                }
            }
        });
    })

});